#include "widget.h"
#include "ui_widget.h"
#include "QMessageBox"
#include "QDebug"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->textEdit_Show->setStyleSheet("background-color: black;");
    ui->textEdit_Show->setTextColor(QColor("white"));


    this->SerialPort = new QSerialPort();
    this->isOpen = false;
    this->searchCom();

    this->readDataTimer = new QTimer();
    connect(this->readDataTimer, &QTimer::timeout, this, &Widget::readDataTimeOut);

    ui->btn_sendData->setDisabled(true);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::readData()
{
    this->readDataTimer->start(10); //TODO:过10ms再接收,数据过大有问题
}

void Widget::readDataTimeOut()
{
    this->readDataTimer->stop();
    this->revBuf = this->SerialPort->readAll();

    /*串口数据显示*/
    if(!this->revBuf.isEmpty())
    {
        if(ui->checkBox_HexShow->isChecked())   //HEX
        {
            QString showHexData;
            QByteArray hexData = this->revBuf.toHex();
            hexData = hexData.toUpper();
            for(int i = 0; i < hexData.length(); i += 2)
            {
                QString byte = hexData.mid(i, 2);
                showHexData += byte;
                showHexData += " ";
            }
            ui->textEdit_Show->append(showHexData);
            qDebug()<< showHexData;
        }
        else
        {
            QString strData = QString::fromLocal8Bit(this->revBuf);
            ui->textEdit_Show->append(strData);
            qDebug()<< strData;
        }
        this->revBuf.clear();   //清除缓存
    }
}

void Widget::on_btn_sendData_clicked()
{
    QString sendData = ui->textEdit_Sned->toPlainText();
    if(ui->checkBox_HexSend->isChecked())   //Hex
    {
        if(sendData.contains(" "))
        {
            sendData.replace(QString(" "), QString(""));    //把空格去掉
        }
        qDebug()<<sendData;
        QByteArray snedBuf = QByteArray::fromHex(sendData.toLatin1());
        this->SerialPort->write(snedBuf);
    }
    else
    {
        this->SerialPort->write(sendData.toLocal8Bit());
    }
}


void Widget::on_btn_clearSend_clicked()
{
    ui->textEdit_Sned->clear();
}

void Widget::on_btn_clearShow_clicked()
{
    ui->textEdit_Show->clear();
}

void Widget::searchCom()
{
    ui->comboBox_comChoose->clear();
    this->portNameList.clear();

    //通过QSerialPortInfo查找可用串口
    foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
    {
        this->portNameList<< info.portName();   //存储串口端口
        //在portBox中显示可用串口
        ui->comboBox_comChoose->addItem(info.portName() + ":" + info.description());
        qDebug()<< info.portName()+ ":" +info.description();
    }
}

void Widget::on_btn_Switch_clicked()
{
    if(this->isOpen == false) //打开串口
    {
        //设置串口号
        this->SerialPort->setPortName(this->portNameList.at(ui->comboBox_comChoose->currentIndex()));

        //打开串口
        if(this->SerialPort->open(QIODevice::ReadWrite))
        {
            this->isOpen = true;
            ui->btn_Switch->setText("关闭串口");
            ui->btn_Switch->setIcon(QIcon(":/icon_open.png"));

            //设置波特率
            this->SerialPort->setBaudRate(ui->comboBox_Baud->currentText().toInt());
            //设置数据位数
            switch(ui->comboBox_DataBit->currentIndex())
            {
                case 0:
                    this->SerialPort->setDataBits(QSerialPort::Data5);
                    break;
                case 1:
                    this->SerialPort->setDataBits(QSerialPort::Data6);
                    break;
                case 2:
                    this->SerialPort->setDataBits(QSerialPort::Data7);
                    break;
                case 3:
                    this->SerialPort->setDataBits(QSerialPort::Data8);
                    break;
                default:
                    break;
            }

            //校验位
            switch(ui->comboBox_ParityBit->currentIndex())
            {
                case 0:
                    this->SerialPort->setParity(QSerialPort::NoParity);
                    break;
                case 1:
                    this->SerialPort->setParity(QSerialPort::OddParity);
                    break;
                case 2:
                    this->SerialPort->setParity(QSerialPort::EvenParity);
                    break;
                default:
                    break;
            }

            //设置流控制
            SerialPort->setFlowControl(QSerialPort::NoFlowControl);

            //设置停止位
            switch(ui->comboBox_ShopBit->currentIndex())
            {
                case 0:
                    SerialPort->setStopBits(QSerialPort::OneStop);
                    break;
                case 1:
                    SerialPort->setStopBits(QSerialPort::OneAndHalfStop);
                    break;
                case 2:
                    SerialPort->setStopBits(QSerialPort::TwoStop);
                    break;
                default:
                    break;
            }
            //设置接收槽
            connect(this->SerialPort, &QSerialPort::readyRead, this, &Widget::readData);

            ui->btn_searchCom->setDisabled(true);   //失能扫描按键
            ui->btn_sendData->setEnabled(true);    //使能发送按键

            ui->comboBox_comChoose->setDisabled(true);  //失能复选框
            ui->comboBox_Baud->setDisabled(true);  //失能复选框
            ui->comboBox_DataBit->setDisabled(true);  //失能复选框
            ui->comboBox_ShopBit->setDisabled(true);  //失能复选框
            ui->comboBox_ParityBit->setDisabled(true);  //失能复选框

        }
        else
        {
            ui->btn_searchCom->setEnabled(true);
            ui->btn_sendData->setDisabled(true);

            ui->comboBox_comChoose->setEnabled(true);
            ui->comboBox_Baud->setEnabled(true);
            ui->comboBox_DataBit->setEnabled(true);
            ui->comboBox_ShopBit->setEnabled(true);
            ui->comboBox_ParityBit->setEnabled(true);

            QMessageBox::about(NULL, "提示", "串口无法打开\r\n不存在或已占用!");
        }
    }
    else    //关闭串口
    {
        this->isOpen = false;
        ui->btn_Switch->setText("打开串口");
        ui->btn_Switch->setIcon(QIcon(":/icon_close.png"));
        this->SerialPort->close();
        disconnect(this->SerialPort, &QSerialPort::readyRead, this, &Widget::readData);

        ui->btn_searchCom->setEnabled(true);
        ui->btn_sendData->setDisabled(true);

        ui->comboBox_comChoose->setEnabled(true);
        ui->comboBox_Baud->setEnabled(true);
        ui->comboBox_DataBit->setEnabled(true);
        ui->comboBox_ShopBit->setEnabled(true);
        ui->comboBox_ParityBit->setEnabled(true);

    }
}

void Widget::on_btn_searchCom_clicked()
{
    if(this->isOpen == false)
    {
        this->searchCom();
    }
}

